import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:freedom/const/game_const.dart';
import 'package:freedom/entity/meta/meta_room_entity.dart';
import 'package:freedom/entity/rule/mahjong_rule.dart';
import 'package:freedom/util/cache_util.dart';
import 'package:freedom/widget/choice_common.dart';

class ChengduMahjong extends StatefulWidget {
  final MetaRoomEntity metaRoomEntity;

  ChengduMahjong(this.metaRoomEntity);

  @override
  _ChengduMahjongState createState() => _ChengduMahjongState();
}

class _ChengduMahjongState extends State<ChengduMahjong> {
  MahjongRule roomEntity = MahjongRule.empty();

  void saveCache() {
    widget.metaRoomEntity.roomRule = roomEntity;
    CacheUtil.preferencesInstance!
        .setString(CacheConst.cdMachjongRuleCache, json.encode(roomEntity));
    setState(() {});
  }

  void _setUserCount(dynamic userCount) {
    roomEntity.userCount = userCount;
    saveCache();
  }

  void _setTurns(dynamic turns) {
    roomEntity.turns = turns;
    saveCache();
  }

  void _setType(dynamic type) {
    roomEntity.type = type;
    saveCache();
  }

  void _setNumberGame(dynamic numberGame) {
    roomEntity.numberGame = numberGame;
    saveCache();
  }

  void _setRules(bool change, String value, List<String> rules) {
    if (change) {
      rules.add(value);
    } else {
      rules.remove(value);
    }
    saveCache();
  }

  @override
  void initState() {
    String? createWzCache = CacheUtil.preferencesInstance!
        .getString(CacheConst.cdMachjongRuleCache);
    if (createWzCache != null) {
      roomEntity = MahjongRule.fromJson(json.decode(createWzCache));
    }
    saveCache();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ChoiceCommon.singleChoice("人数:", [
          ChoiceCommon.buildRadio(2, "2人", roomEntity.userCount, _setUserCount),
          ChoiceCommon.buildRadio(3, "3人", roomEntity.userCount, _setUserCount),
          ChoiceCommon.buildRadio(4, "4人", roomEntity.userCount, _setUserCount),
        ]),
        ChoiceCommon.singleChoice("局数:", [
          ChoiceCommon.buildRadio(
              2, "2局", roomEntity.numberGame, _setNumberGame),
          ChoiceCommon.buildRadio(
              4, "4局", roomEntity.numberGame, _setNumberGame),
          ChoiceCommon.buildRadio(
              8, "8局", roomEntity.numberGame, _setNumberGame),
          ChoiceCommon.buildRadio(
              16, "16局", roomEntity.numberGame, _setNumberGame),
        ]),
        ChoiceCommon.singleChoice("封顶:", [
          ChoiceCommon.buildRadio(3, "3番", roomEntity.turns, _setTurns),
          ChoiceCommon.buildRadio(4, "4番", roomEntity.turns, _setTurns),
          ChoiceCommon.buildRadio(5, "5番", roomEntity.turns, _setTurns),
          ChoiceCommon.buildRadio(null, "不封顶", roomEntity.turns, _setTurns),
        ]),
        ChoiceCommon.singleChoice("玩法:", [
          ChoiceCommon.buildRadio(1, "血战模式", roomEntity.type, _setType),
          ChoiceCommon.buildRadio(2, "非血战模式", roomEntity.type, _setType),
        ]),
        ChoiceCommon.multipleChoice("", [
          ChoiceCommon.buildMultipleRadio(
              "HSZ", "换三张", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "HJZY", "呼叫转移", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "CG", "擦挂", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "GSJFKH", "过手加番可胡", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "LZ", "轮庄", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "GZGH", "过庄过胡", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "TDH", "天地胡", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "SJBSJ", "死叫不算叫", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "GPGQ", "过碰过圈", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "YHBH", "有胡必胡", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "LFQH", "两分起胡", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "DDHFLF", "对对胡翻两番", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "JXW", "夹心五", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "JZJF", "绝张加番", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "LZWF", "癞子玩法", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "WLZJF", "无癞子加番", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "YTL", "一条龙", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "JSY", "及时雨", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "MQZZ", "门清中张", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "YJJD", "幺九将对", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "XSLS", "显示流水", roomEntity.rules, _setRules),
          ChoiceCommon.buildMultipleRadio(
              "CHZXZ", "查花猪限制", roomEntity.rules, _setRules),
        ])
      ],
    );
  }
}
